﻿using System;
using System.Collections.Generic;
//using System.Linq;
using System.Text;
using System.IO;
using System.Windows.Forms;

using Microsoft.Win32;
using System.Threading;
using QuanLyKho.GUI.Properties;
using System.Drawing;
using System.Security.Cryptography;

namespace QuanLyKho.GUI.Library
{
    public class clsLibrary
    {
        public string formatDateOfWeek(DayOfWeek _dayOfWeek)
        {
            string dayOfWeek = string.Empty;
            switch (_dayOfWeek)
            {
                case DayOfWeek.Monday:
                    dayOfWeek = "2";
                    break;
                case DayOfWeek.Tuesday:
                    dayOfWeek = "3";
                    break;
                case DayOfWeek.Wednesday:
                    dayOfWeek = "4";
                    break;
                case DayOfWeek.Thursday:
                    dayOfWeek = "5";
                    break;
                case DayOfWeek.Friday:
                    dayOfWeek = "6";
                    break;
                case DayOfWeek.Saturday:
                    dayOfWeek = "7";
                    break;
            }
            return dayOfWeek;
        }

        public DateTime convertDateTime(string datetime)
        {
            DateTime value = new DateTime();
            string[] splTime = datetime.Split('/');
            string s = splTime[1] + "/" + splTime[0] + "/" + splTime[2];
            try
            {
                value = DateTime.Parse(s);
            }
            catch
            {
                value = DateTime.Parse(datetime);
            }
            return value;
        }

        public void processPanel(UserControl uc, Panel pn)
        {
            pn.Controls.Clear();
            uc.Dock = DockStyle.Fill;
            pn.Controls.Add(uc);
        }

        public void processGroupBox(UserControl uc, GroupBox pn)
        {
            pn.Controls.Clear();
            uc.Dock = DockStyle.Fill;
            pn.Controls.Add(uc);
        }
        public void process2GroupBox(GroupBox uc, GroupBox pn)
        {
            pn.Controls.Clear();
            uc.Dock = DockStyle.Fill;
            pn.Controls.Add(uc);
        }
      

        public void processTreview(TreeView tr, GroupBox pn)
        {
            pn.Controls.Clear();
            tr.Dock = DockStyle.Fill;
            pn.Controls.Add(tr);
        }
        public static string convetToDouble(double num)
        {
            if (double.IsNaN(num))
                return "0";

            double a = 0;
            string n = a.ToString();
            if (double.TryParse(num.ToString(), out a))
                n = Math.Round(a, 1).ToString();
            return n;
        }

        public string EnCrypt(string strEnCrypt, string key)
        {
            try
            {
                byte[] keyArr;
                byte[] EnCryptArr = UTF8Encoding.UTF8.GetBytes(strEnCrypt);
                MD5CryptoServiceProvider MD5Hash = new MD5CryptoServiceProvider();
                keyArr = MD5Hash.ComputeHash(UTF8Encoding.UTF8.GetBytes(key));
                TripleDESCryptoServiceProvider tripDes = new TripleDESCryptoServiceProvider();
                tripDes.Key = keyArr;
                tripDes.Mode = CipherMode.ECB;
                tripDes.Padding = PaddingMode.PKCS7;
                ICryptoTransform transform = tripDes.CreateEncryptor();
                byte[] arrResult = transform.TransformFinalBlock(EnCryptArr, 0, EnCryptArr.Length);
                return Convert.ToBase64String(arrResult, 0, arrResult.Length);
            }
            catch (Exception ex) { }
            return "";
        }

        public string DeCrypt(string strDecypt, string key)
        {
            try
            {
                byte[] keyArr;
                byte[] DeCryptArr = Convert.FromBase64String(strDecypt);
                MD5CryptoServiceProvider MD5Hash = new MD5CryptoServiceProvider();
                keyArr = MD5Hash.ComputeHash(UTF8Encoding.UTF8.GetBytes(key));
                TripleDESCryptoServiceProvider tripDes = new TripleDESCryptoServiceProvider();
                tripDes.Key = keyArr;
                tripDes.Mode = CipherMode.ECB;
                tripDes.Padding = PaddingMode.PKCS7;
                ICryptoTransform transform = tripDes.CreateDecryptor();
                byte[] arrResult = transform.TransformFinalBlock(DeCryptArr, 0, DeCryptArr.Length);
                return UTF8Encoding.UTF8.GetString(arrResult);
            }
            catch (Exception ex) { }
            return "";
        }


        public string ConvertBytesToString(byte[] bytes)
        {
            string output = string.Empty;
            MemoryStream ms = new MemoryStream(bytes);
            ms.Position = 0;
            using (StreamReader reader = new StreamReader(ms))
            {
                output = reader.ReadToEnd();
            }
            return output;
        }

        public byte[] ConvertStringToBytes(string input)
        {
            MemoryStream ms = new MemoryStream();
            using (StreamWriter writer = new StreamWriter(ms))
            {
                writer.Write(input);
                writer.Flush();
            }
            return ms.ToArray();
        }

        /// <summary>
        /// Lua chon duong dan de luu file
        /// </summary>
        /// <param name="initialDirectory">string:: ten thu muc</param>
        /// <param name="_fileName">string:: ten file can luu</param>
        /// <param name="_filter">string:: dinh dang file can luu</param>
        /// <returns></returns>
        public string SaveDialogFile(string initialDirectory, string _fileName, string _filter)
        {
            SaveFileDialog saveFileDialog1 = new SaveFileDialog();
            saveFileDialog1.Filter = "Save file (*." + _filter + ")|*." + _filter + "";
            saveFileDialog1.RestoreDirectory = true;
            saveFileDialog1.InitialDirectory = initialDirectory;
            saveFileDialog1.Title = "Lựa chọn nơi lưu.";
            if (!string.IsNullOrEmpty(_fileName))
                saveFileDialog1.FileName = _fileName + "." + _filter;

            return (saveFileDialog1.ShowDialog() == DialogResult.OK) ? saveFileDialog1.FileName : null;
        }

        /// <summary>
        /// Lua chon duong dan thu muc chua file db (Attach, restore..)
        /// </summary>
        /// <param name="initialDirectory">string:: ten thu muc</param>
        /// <param name="flag">bool:: kieu Restore hoac Attach</param>
        /// <returns>string:: duong dan file tuyet doi</returns>
        public string SelectTextFile(string initialDirectory, bool flag)
        {
            OpenFileDialog dialog = new OpenFileDialog();
            dialog.InitialDirectory = initialDirectory;
            if (flag)
            {
                dialog.Title = "Lựa chọn file Attach Database.";
                dialog.Filter = "Sql files (*.mdf)|*.mdf|All files (*.*)|*.*";
            }
            else
            {
                dialog.Title = "Lựa chọn file backup.";
                dialog.Filter = "Backup files (*.bak)|*.bak|All files (*.*)|*.*";
            }

            return (dialog.ShowDialog() == DialogResult.OK)
                  ? dialog.FileName : null;
        }

        public Image imgLoading()
        {
            return global::QuanLyKho.GUI.Properties.Resources.ajax_loader;
        }

        public void runLoading(object obj, int status)
        {
            if (obj != null)
            {
                string s = (string)obj;
                frmMain.lblTitile.Text = s;
            }
            frmMain.lblTitile.Visible = true;
            frmMain.pnLoading.Visible = true;
            frmMain.pbLoading.Visible = true;

            frmMain.timerLoading.Interval = 1000;
            switch (status)
            {
                case 0:
                    frmMain.pbLoading.Image = global::QuanLyKho.GUI.Properties.Resources.Cancel;
                    break;
                case 2:
                    frmMain.pbLoading.Image = global::QuanLyKho.GUI.Properties.Resources.Valid;
                    break;
                default:
                    frmMain.pbLoading.Image = global::QuanLyKho.GUI.Properties.Resources.ajax_loader;
                    break;
            }
        }

        public void disposeLoading()
        {
            frmMain.timerLoading.Enabled = false;
        }

        public void runMain()
        {
            try
            {
                Application.Run(new frmMain());
            }
            catch (Exception ex)
            {
                if (!(ex is ThreadAbortException))
                {
                    MessageBox.Show(ex.ToString());
                    //Do exception handling.
                }
            }
        }

        public void RunConnect()
        {
            try
            {
                Application.Run(new frmConnect());
            }
            catch (Exception ex)
            {
                if (!(ex is ThreadAbortException))
                {
                    //MessageBox.Show(ex.ToString());
                    //Do exception handling.
                }
            }
        }

        public void runLogin()
        {
            try
            {
                Application.Run(new frmLogin());
            }
            catch (Exception ex)
            {
                if (!(ex is ThreadAbortException))
                {
                    //MessageBox.Show(ex.ToString());
                    //Do exception handling.
                }
            }
        }

        public void runRestore()
        {
            try
            {
                Application.Run(new frmRestore());
            }
            catch (Exception ex)
            {
                if (!(ex is ThreadAbortException))
                {
                    //MessageBox.Show(ex.ToString());
                    //Do exception handling.
                }
            }
        }
    }
}
